package com.broada.apm.base;


import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;

import com.broada.apm.indicator.api.AlertService;
import com.broada.apm.indicator.api.AnalyzerService;
import com.broada.apm.indicator.api.ApmClientFactory;
import com.broada.apm.indicator.api.AppDomainService;
import com.broada.apm.indicator.api.ApplicationService;
import com.broada.apm.indicator.api.ConfigItemService;
import com.broada.apm.indicator.api.DimensionService;
import com.broada.apm.indicator.api.ErrorRecordService;
import com.broada.apm.indicator.api.IPLocationService;
import com.broada.apm.indicator.api.IndicatorItemService;
import com.broada.apm.indicator.api.L7ModuleService;
import com.broada.apm.indicator.api.L7moduleMenuService;
import com.broada.apm.indicator.api.LuaSettingsService;
import com.broada.apm.indicator.api.PerformancePersistanceService;
import com.broada.apm.indicator.api.PerformanceQueryService;
import com.broada.apm.indicator.api.RelationService;
import com.broada.apm.indicator.api.ResourceService;
import com.broada.apm.indicator.api.ServiceService;
import com.broada.apm.indicator.api.TransactionService;
import com.broada.apm.indicator.api.UserService;
import com.broada.apm.indicator.api.util.AggregationUtil;
import com.broada.apm.model.DictMetrics;
import com.broada.apm.service.internal.IndicatorService;
import com.broada.apm.service.internal.impl.IndicatorServiceImpl;

public class BaseLogic {

//	public QueryService queryService = ApmClientFactory.getApmClient().getQueryService();
	public PerformanceQueryService queryPerfService = ApmClientFactory.getApmClient().getPerformanceQueryService();
	public PerformancePersistanceService perService = ApmClientFactory.getApmClient().getPerformancePersistanceService();
	public IndicatorService indicatorService = new IndicatorServiceImpl();
	public ApplicationService applicationService = ApmClientFactory.getApmClient().getApplicationService();
	public ServiceService serviceService = ApmClientFactory.getApmClient().getServiceService();
	public RelationService relationService = ApmClientFactory.getApmClient().getRelationService();
	public ConfigItemService configItemService = ApmClientFactory.getApmClient().getConfigItemService();
	public ResourceService resourceService = ApmClientFactory.getApmClient().getResourceService();
	public AlertService alertService = ApmClientFactory.getApmClient().getAlertService();
	public IPLocationService ipLocationService = ApmClientFactory.getApmClient().getIpLocationService();
	public L7ModuleService l7ModuleService = ApmClientFactory.getApmClient().getL7ModuleService();
	public L7moduleMenuService l7ModuleMenuService = ApmClientFactory.getApmClient().getL7moduleMenuService();
	public ErrorRecordService errorRecordService = ApmClientFactory.getApmClient().getErrorRecordService();
	public UserService userService = ApmClientFactory.getApmClient().getUserService();
	public DimensionService dimensionService = ApmClientFactory.getApmClient().getDimensionService();
	public AppDomainService appDomainService = ApmClientFactory.getApmClient().getAppDomainService();
	public IndicatorItemService indicatorItemService = ApmClientFactory.getApmClient().getIndicatorItemService();
	public TransactionService transactionService = ApmClientFactory.getApmClient().getTransactionService();
	public AnalyzerService analyzerService = ApmClientFactory.getApmClient().getAnalyzerService();
	public LuaSettingsService luaSettingsService = ApmClientFactory.getApmClient().getLuaSettingsService();
	public static final String DESC = "DESC";
	public static final String ASC = "ASC";

	protected void setSubAggregation(TermsBuilder aggs, DictMetrics[] items) {
		for (DictMetrics indicatorItem : items) {
			if (indicatorItem.getCode().equals("system/response_time/avg")) {
				aggs.subAggregation(AggregationUtil.getSum("system/response_time/total", "system/response_time/total"));
				aggs.subAggregation(AggregationUtil.getAvg("system/response_time/avg", "system/response_time/avg"));
			} else {
				switch (indicatorItem.getCollectionAlogithm()) {
				case DictMetrics.OPERATOR_SUM:
					aggs.subAggregation(AggregationUtil.getSum(indicatorItem.getCode(), indicatorItem.getCode()));
					break;
				case DictMetrics.OPERATOR_AVG:
					aggs.subAggregation(AggregationUtil.getAvg(indicatorItem.getCode(), indicatorItem.getCode()));
					break;
				case DictMetrics.OPERATOR_MIN:
					aggs.subAggregation(AggregationUtil.getMin(indicatorItem.getCode(), indicatorItem.getCode()));
					break;
				case DictMetrics.OPERATOR_MAX:
					aggs.subAggregation(AggregationUtil.getMax(indicatorItem.getCode(), indicatorItem.getCode()));
					break;
				default:
					aggs.subAggregation(AggregationUtil.getSum(indicatorItem.getCode(), indicatorItem.getCode()));
					break;
				}
			}
		}
	}
}
